package com.xinxin.leetcode.problem46;

import com.xinxin.leetcode.common.CommonUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ShiXinXin
 * @date 2020-03-03 14:45
 */
public class Solution {
    private List<List<Integer>> res = new ArrayList<>();

    private void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    private void permute(int[] nums, int index) {
        if (index == nums.length - 1) {
            res.add(Arrays.stream(nums).boxed().collect(Collectors.toList()));
        }
        for (int i = index; i < nums.length; i++) {
            swap(nums, index, i);
            permute(nums, index + 1);
            swap(nums, index, i);
        }
    }

    public List<List<Integer>> permute(int[] nums) {
        permute(nums, 0);
        return res;
    }

    public static void main(String[] args) {
        System.out.println(new Solution().permute(CommonUtil.createArr(1, 2, 3)));
    }
}
